package com.example.sunmiv1.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.sunmiv1.R;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.example.sunmiv1.utils.zxing.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收銀員權限碼彈窗
 */

public class AddMemberLabelDialog extends BaseDialog {


    @BindView(R.id.addMemberLabel_title)
    TextView addMemberLabelTitle;
    @BindView(R.id.addMemberLabel_off)
    ImageView addMemberLabelOff;
    @BindView(R.id.addMemberLabel_phone)
    EditText addMemberLabelPhone;
    @BindView(R.id.addMemberLabel_confirm)
    TextView addMemberLabelConfirm;
    private Context context;

    private String labelId;//1添加 2編輯

    public static AddMemberLabelDialog newInstance(String labelId) {
        AddMemberLabelDialog fragment = new AddMemberLabelDialog();
        Bundle bundle = new Bundle();
        bundle.putString("labelId", labelId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        context = getActivity();
        return R.layout.dialog_add_member_label;
    }


    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            // 下面這些設置必須在此方法(onStart())中才有效
            Window window = dialog.getWindow();
            // 如果不設置這句代碼, 那麼彈框就會與四邊都有一定的距離
            window.setBackgroundDrawableResource(android.R.color.transparent);
            window.setWindowAnimations(R.style.DialogFromBottom);// 設置動畫
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = dp2px(350);
            params.gravity = Gravity.BOTTOM;
            window.setAttributes(setLayout(params));
        }
    }

    @Override
    protected void initData() {
        labelId = getArguments().getString("labelId");
        if (TextUtils.isEmpty(labelId)) {
            addMemberLabelTitle.setText("添加標籤");
        } else {
            addMemberLabelTitle.setText("編輯標籤");
        }
    }

    @OnClick({R.id.addMemberLabel_off, R.id.addMemberLabel_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.addMemberLabel_off:
                dismiss();
                break;
            case R.id.addMemberLabel_confirm:
                if (TextUtils.isEmpty(addMemberLabelPhone.getText().toString().trim())) {
                    Toast.makeText(context, "請輸入標籤", Toast.LENGTH_SHORT).show();
                    return;
                }
                addMemberLabel();
                break;
        }
    }

    private void addMemberLabel() {
        JSONObject jsonObject = new JSONObject();
        String url;
        try {
            jsonObject.put("name", addMemberLabelPhone.getText().toString().trim());
            if (TextUtils.isEmpty(labelId)) {//添加
                url = Common.ADD_VIP_LABEL;
            } else {//編輯
                jsonObject.put("label_id", labelId);
                url = Common.EDIT_VIP_LABEL;
            }
            jsonObject = Tools.createParameterCode(jsonObject);
            HttpUtils.post(context, url, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    try {
                        JSONObject json = new JSONObject(text);
                        Toast.makeText(context, json.getString("msg"), Toast.LENGTH_SHORT).show();
                        if (json.getInt("status") == 1) {
                            callback.success("");
                            dismiss();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
